package com.jeethink.crm.service.impl;

import java.util.List;
import com.jeethink.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeethink.crm.mapper.CrmOrderMapper;
import com.jeethink.crm.mapper.FinancePayMapper;
import com.jeethink.crm.domain.CrmOrder;
import com.jeethink.crm.domain.FinancePay;
import com.jeethink.crm.service.IFinancePayService;
import com.jeethink.common.annotation.DataScope;
import com.jeethink.common.core.text.Convert;
import com.jeethink.common.exception.BusinessException;

/**
 * 回款管理Service业务层处理
 * 
 * @author jeethink
 * @date 2020-04-12
 */
@Service
public class FinancePayServiceImpl implements IFinancePayService 
{
    @Autowired
    private FinancePayMapper financePayMapper;
    @Autowired
    private CrmOrderMapper crmOrderMapper;

    /**
     * 查询回款管理
     * 
     * @param payId 回款管理ID
     * @return 回款管理
     */
    @Override
    public FinancePay selectFinancePayById(Long payId)
    {
        return financePayMapper.selectFinancePayById(payId);
    }

    /**
     * 查询回款管理列表
     * 
     * @param financePay 回款管理
     * @return 回款管理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<FinancePay> selectFinancePayList(FinancePay financePay)
    {
        return financePayMapper.selectFinancePayList(financePay);
    }

    /**
     * 新增回款管理
     * 
     * @param financePay 回款管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFinancePay(FinancePay financePay)
    {
        financePay.setCreateTime(DateUtils.getNowDate());
        Double payMoney = financePayMapper.selectPayMoneyByOrderId(financePay.getOrderId());
        CrmOrder order=crmOrderMapper.selectCrmOrderById(financePay.getOrderId());
        Double orderPrice =order.getTotalPrice();
        if((payMoney+financePay.getPayMoney())>orderPrice) {
            throw new BusinessException("合计回款金额不能大于订单金额！");
        }
        //更改订单状态（已部分回款）
        if((payMoney+financePay.getPayMoney())<orderPrice) {
        	order.setOrderStatus("2");//已部分回款
        }
        else {
        	order.setOrderStatus("3");//已全部回款        	
        }
    	order.setUpdateBy(financePay.getCreateBy());
    	order.setUpdateTime(financePay.getCreateTime());
    	crmOrderMapper.updateCrmOrder(order);
        return financePayMapper.insertFinancePay(financePay);
    }

    /**
     * 修改回款管理
     * 
     * @param financePay 回款管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFinancePay(FinancePay financePay)
    {
        financePay.setUpdateTime(DateUtils.getNowDate());
    	FinancePay oldFinancePay=financePayMapper.selectFinancePayById(financePay.getPayId());
        Double payMoney = financePayMapper.selectPayMoneyByOrderId(financePay.getOrderId());
        CrmOrder order=crmOrderMapper.selectCrmOrderById(financePay.getOrderId());
        Double orderPrice =order.getTotalPrice();
        if((payMoney-oldFinancePay.getPayMoney()+financePay.getPayMoney())>orderPrice) {
            throw new BusinessException("合计回款金额不能大于订单金额！");
        }
        //更改订单状态（已部分回款）
        if((payMoney-oldFinancePay.getPayMoney()+financePay.getPayMoney())<orderPrice) {
        	order.setOrderStatus("2");//已部分回款
        }
        else {
        	order.setOrderStatus("3");//已全部回款        	
        }
    	order.setUpdateBy(financePay.getUpdateBy());
    	order.setUpdateTime(financePay.getUpdateTime());
    	crmOrderMapper.updateCrmOrder(order);        
        return financePayMapper.updateFinancePay(financePay);
    }

    /**
     * 删除回款管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFinancePayByIds(String ids)
    {
    	Long[] payByIds = Convert.toLongArray(ids);
    	for(int j=0;j< payByIds.length;j++){  
    		FinancePay financePay=financePayMapper.selectFinancePayById(payByIds[j]);
            Double payMoney = financePayMapper.selectPayMoneyByOrderId(financePay.getOrderId());
            CrmOrder order=crmOrderMapper.selectCrmOrderById(financePay.getOrderId());
            Double orderPrice =order.getTotalPrice();
            if((payMoney-financePay.getPayMoney())==0.0) {
            	order.setOrderStatus("1");//未付款
            }
            //更改订单状态（已部分回款）
            else if((payMoney-financePay.getPayMoney())<orderPrice) {
            	order.setOrderStatus("2");//已部分回款
            }
        	order.setUpdateBy(financePay.getUpdateBy());
        	order.setUpdateTime(DateUtils.getNowDate());
        	crmOrderMapper.updateCrmOrder(order);       
    	}
    	return financePayMapper.deleteFinancePayByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除回款管理信息
     * 
     * @param payId 回款管理ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFinancePayById(Long payId)
    {
    	FinancePay financePay=financePayMapper.selectFinancePayById(payId);
        Double payMoney = financePayMapper.selectPayMoneyByOrderId(financePay.getOrderId());
        CrmOrder order=crmOrderMapper.selectCrmOrderById(financePay.getOrderId());
        Double orderPrice =order.getTotalPrice();
        if((payMoney-financePay.getPayMoney())==0.0) {
        	order.setOrderStatus("1");//未付款
        }
        //更改订单状态（已部分回款）
        else if((payMoney-financePay.getPayMoney())<orderPrice) {
        	order.setOrderStatus("2");//已部分回款
        }
    	order.setUpdateBy(financePay.getUpdateBy());
    	order.setUpdateTime(DateUtils.getNowDate());
    	crmOrderMapper.updateCrmOrder(order);        
        return financePayMapper.deleteFinancePayById(payId);
    }
    
    /**
     * 查询回款合计
     * 
     * @param orderId 订单id
     * @return 合计
     */
    @Override
    public Double selectPayMoneyByOrderId(Long orderId) {
    	return financePayMapper.selectPayMoneyByOrderId(orderId);
    }
}
